<?php

namespace App\Library\Rms;

use App\Enums\Pay\RmsPayEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use App\Service\BaseService;

class RmsPayApi extends RmsBaseApi
{
    private ?int    $sysType;
    private string $callbackUrl;

    public function __construct(?int $sysType)
    {
        parent::__construct();

        $this->sysType     = $sysType;
        $this->callbackUrl = config('app.url') . 'callback/pay?sysType=' . $this->sysType;
    }

    /**
     * H5支付
     * @param int $orderId 订单号
     * @param string $subject 备注
     * @param int $amount 金额，单位分
     * @param int $payChannel 1支付宝 2微信
     * @param string $h5BackUrl 支付完成后，回跳的H5 url
     * @param int $shopId 店铺id，没店铺给0
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function wapPay(
        int $orderId,
        string $subject,
        int $amount,
        int $payChannel,
        string $h5BackUrl,
        int $shopId,
        string $bizId
    ): array {
        $params = [
            'orderId'     => $orderId,
            'subject'     => $subject,
            'amount'      => $amount,
            'payChannel'  => $payChannel,
            'callbackUrl' => $this->callbackUrl,
            'h5BackUrl'   => $h5BackUrl,
            'shopId'      => $shopId,
            'sysType'     => $this->sysType,
            'bizId'       => $bizId,
        ];

        return $this->rmsCurl->do('api/internal/pay/wapPay', $params, 'POST');
    }

    /**
     * app支付
     * @param int $orderId
     * @param string $subject
     * @param int $amount
     * @param int $payChannel
     * @param int $shopId
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function appPay(
        int $orderId,
        string $subject,
        int $amount,
        int $payChannel,
        int $shopId,
        string $bizId
    ): array {
        $params = [
            'orderId'     => $orderId,
            'subject'     => $subject,
            'amount'      => $amount,
            'payChannel'  => $payChannel,
            'callbackUrl' => $this->callbackUrl,
            'shopId'      => $shopId,
            'sysType'     => $this->sysType,
            'bizId'       => $bizId,
        ];

        return $this->rmsCurl->do('api/internal/pay/directAppPay', $params, 'POST');
    }

    /**
     * 退款
     * @param int $orderId 订单号
     * @param string $subject 备注
     * @param int $amount 金额，单位分
     * @param int $shopId 店铺id
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function refund(int $orderId, string $subject, int $amount, int $shopId): array
    {
        $params = [
            'orderId'     => $orderId,
            'subject'     => $subject,
            'amount'      => $amount,
            'callbackUrl' => $this->callbackUrl,
            'shopId'      => $shopId,
        ];

        return $this->rmsCurl->do('api/internal/pay/directAppRefund', $params, 'POST');
    }

    /**
     * 批量查询支付详情
     * @param array $sysTypes
     * @param int $startTime
     * @param int $endTime
     * @param int $pageSize
     * @param int $pageNum
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function payDetails(array $sysTypes, int $startTime, int $endTime, int $pageNum, int $pageSize)
    {
        $params = [
            'sysTypes'  => $sysTypes,
            'pageSize'  => $pageSize,
            'pageNum'   => $pageNum,
            'startTime' => $startTime,
            'endTime'   => $endTime,
        ];

        return $this->rmsCurl->do('api/internal/pay/payDetails', $params, 'GET');
    }

    /**
     * 获取对账文件url
     * @param int $date
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getCheckFileUrl(int $date)
    {
        $params = [
            'date' => $date,
        ];

        return $this->rmsCurl->do('api/internal/pay/getCheckFileUrl', $params, 'GET');
    }

    /**
     * 新增门店的网商分账配置
     * @param int $shopId
     * @param array $config
     */
    public function addMyBankShareConfig(string $orderNo)
    {
        $shopId = (int)OpenShopMainProcessModel::query()->where('orderNo', $orderNo)->value('shopId');
        if (!$shopId) {
            return BaseService::returnData(0, 'shopId=0');
        }

        $userContract = OpenShopUserContractModel::query()->where('orderNo', $orderNo)->first();
        if (!$userContract) {
            return BaseService::returnData(0, '找不到合同');
        }

        $config = $userContract->getMyBankShareConfig();
        if (!$config) {
            return BaseService::returnData(0, '合同类型不是网商分账');
        }

        $params = [
            'shopId' => $shopId,
            'config' => $config,
        ];

        return $this->rmsCurl->do('api/internal/pay/addMyBankShareConfig', $params, 'POST');
    }
}
